/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.admin.rest;

import static com.automvc.common.service.CommService.isNullPara;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;

import com.aiteasoft.school.admin.entity.Assignexam;
import com.aiteasoft.school.admin.service.AssignexamService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-21 10:42:28
 */
@RestController
@RequestMapping("assignexam")
public class AssignexamRest {
	@Autowired
	ObjSQLService objSQLService;
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	AssignexamService assignexamService;

	@RequestMapping("/list")
	public Result list(Assignexam assignexam, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

		Result result = new Result();
		if (isNullPara(result, assignexam)) return result;
		
		assignexam = assignexamService.process(assignexam);
		
		PageWarp pageWarp = assignexamService.countAndSelect(assignexam, (page - 1) * rows, rows);
		result.setTotal(pageWarp.getTotal());
		result.setRows(pageWarp.getList());

		return result;
	}

	@RequestMapping("/del")
	public Result delete(String ids) {

		Result result = new Result();
		if (isNullPara(result, ids, "ids")) return result;

		int num = objSQLRichService.deleteById(Assignexam.class, ids);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("delete failed!");
		return result;
	}
	
	@RequestMapping("/init")
	public Result init(String classno) {

		Result result = new Result();
		
		int num=assignexamService.initExam(classno);
		if(num==-2) {
			result.setMsg("无初始化数据!");
			//TODO 添加编码给前端用
		}else {
			result.setMsg("已初始化数据 "+ num +" 条!");
		}
		
		return result;
	}
}